package com.kfg.today;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.kfg.sql.Dao;

import android.R.anim;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ModifyActivity extends Activity {
	EditText titlev, contentv;
	TextView timev;
	ImageButton cancel, save, back;
	String title, content, time, id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modify);
		titlev = (EditText) findViewById(R.id.tit1);
		contentv = (EditText) findViewById(R.id.edt1);
		contentv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (back.isShown()) {
					back.setVisibility(View.INVISIBLE);
					cancel.setVisibility(View.VISIBLE);
					save.setVisibility(View.VISIBLE);
				}
				if (!contentv.isFocusableInTouchMode()) {
					contentv.setFocusable(true);
					contentv.setFocusableInTouchMode(true);
					contentv.requestFocus();
					InputMethodManager im = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
					im.showSoftInput(contentv, 0);
				}
			}
		});
		titlev.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (back.isShown()) {
					back.setVisibility(View.INVISIBLE);
					cancel.setVisibility(View.VISIBLE);
					save.setVisibility(View.VISIBLE);
				}
				if (!titlev.isFocusableInTouchMode()) {
					titlev.setFocusable(true);
					titlev.setFocusableInTouchMode(true);
					titlev.requestFocus();
					titlev.setInputType(InputType.TYPE_CLASS_TEXT);
					InputMethodManager im = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
					im.showSoftInput(titlev, 0);
				}
			}
		});
		id = getIntent().getStringExtra("id");
		Dao dao = new Dao(this);
		Map<String, String> item = new HashMap<String, String>();
		item = dao.getbyid(id);
		title = item.get("title");
		content = item.get("content");
		time = item.get("time");
		Log.i("b", title + content + time);
		titlev.setText(title);
		contentv.setText(content);
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
				ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
		LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View titlebar = inflator.inflate(R.layout.actionbar_modify, new LinearLayout(ModifyActivity.this), false);
		getActionBar().setCustomView(titlebar, lp);
		getActionBar().setDisplayShowHomeEnabled(false);
		getActionBar().setDisplayShowTitleEnabled(false);
		getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		getActionBar().setDisplayShowCustomEnabled(true);
		timev = (TextView) titlebar.findViewById(R.id.time);
		timev.setText(time);
		back = (ImageButton) titlebar.findViewById(R.id.back);
		cancel = (ImageButton) titlebar.findViewById(R.id.cancel);
		save = (ImageButton) titlebar.findViewById(R.id.save);
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!contentv.getText().toString().equals("")) {
					Dao dao = new Dao(ModifyActivity.this);
					Log.i("e", titlev.getText().toString()+contentv.getText().toString()+id);
					dao.update(titlev.getText().toString(), contentv.getText().toString(), id);
					Intent i=new Intent(ModifyActivity.this,AllTextActivity.class);
					startActivity(i);
					overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
					finish();
				}
			}
		});
		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				contentv.setFocusable(false);
				titlev.setFocusable(false);
				back.setVisibility(View.VISIBLE);
				cancel.setVisibility(View.INVISIBLE);
				save.setVisibility(View.INVISIBLE);
				titlev.setText(title);
				contentv.setText(content);
			}
		});
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i=new Intent(ModifyActivity.this,AllTextActivity.class);
				startActivity(i);
				overridePendingTransition(R.anim.left_to_right,R.anim.push_right_out);
				finish();
			}
		});
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (cancel.isShown()) {
			contentv.setFocusable(false);
			titlev.setFocusable(false);
			back.setVisibility(View.VISIBLE);
			cancel.setVisibility(View.INVISIBLE);
			save.setVisibility(View.INVISIBLE);
		} else {
			Intent i=new Intent(ModifyActivity.this,AllTextActivity.class);
			startActivity(i);
			finish();
			overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
		}
	}
}
